<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>首页</title>
</head>
<body>

<h2 align="center">人事管理系统</h2>

<table align="center" border="1">
    <tr>
        <th><input type="checkbox" id="all" name="all" onclick="chooseAll()">全选</th>
        <th>用户编号</th>
        <th>姓名</th>
        <th>账户</th>
        <th>密码</th>
        <th>年龄</th>
        <th>住址</th>
        <th>操作</th>
    </tr>
    <c:forEach items="${users}" var="user">
        <tr>
            <td><p align="center"><input type="checkbox" name="single" value="${user.uid}"></p></td>
            <td>${user.uid}</td>
            <td>${user.name}</td>
            <td>${user.username}</td>
            <td>${user.password}</td>
            <td>${user.age}</td>
            <td>${user.address}</td>
            <td>
                <a href="${pageContext.request.contextPath}/user/delete.do?&ids=${user.uid}" onclick="confirm('是否删除选中用户?')">删除</a>
                <a href="${pageContext.request.contextPath}/user/goUpdate.do?&uid=${user.uid}">修改</a>
            </td>
        </tr>
    </c:forEach>

</table>
<p align="center">
    <input type="button" onclick="location.href='${pageContext.request.contextPath}/user/regist.do'" value="注册">
    <input type="button" id="chooseDel" value="删除">
</p>
</body>

<script>

    let ORIGIN_PATH = "${pageContext.request.contextPath}";
    let DEL_PATH = "/user/delete.do?method=delete";
    let chooseDel = document.querySelector("#chooseDel");
    let all = document.getElementsByName("all");
    let singles = document.getElementsByName("single");

    chooseDel.addEventListener("click", e=> {
        if (confirm("是否删除选中用户?")) {
            singles.forEach(e => {
                if(e.checked){
                    DEL_PATH = DEL_PATH + "&ids=" + e.value
                }
            })
        }
        location.href = ORIGIN_PATH + DEL_PATH;
    })

    function chooseAll(){
        singles.forEach(e=>{
            e.checked = all[0].checked
        })
    }

</script>

</html>
